from urllib import request,parse
#用post请求 模拟登录微博
#post请求; 如果要以POST发送一个请求，只需要把参数data以bytes形式传入。

email = input('email:')
psw = input('psw:')

login_data = parse.urlencode(
[
	('username',email),
	('password',psw),
	('entry','mweibo'),
	('client_id',''),
	('savestate','1'),
	('ec',''),
	('pagerefer','https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')
]
)
#设置请求头文件
req = request.Request('https://passport.weibo.cn/sso/login')
req.add_header('Origin', 'https://passport.weibo.cn')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')

#通过login_data.encode('utf-8')把数据转换成字节码传入
with request.urlopen(req,data=login_data.encode('utf-8')) as f:
	print('status:',f.status,f.reason)
	print('post data:',f.read().decode('utf-8'))